import {Toast,Loading} from 'vue-ydui/dist/lib.rem/dialog';
const getCookie=function(name)
{
  let arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

  if(arr=document.cookie.match(reg))

    return unescape(arr[2]).replace('j:','').replace('"','').replace('"','').replace('u_id','"u_id"').toString();
  else
    return null;
};

const  getUA=function(){

  var u = navigator.userAgent;
  var UA=null;
  if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
    UA = "Android";
  } else if (u.indexOf('iPhone') > -1) {//苹果手机
    UA = "iPhone";
  } else if (u.indexOf('Windows Phone') > -1) {//winphone手机
    UA = "winPhone";
  }
  else if (u.indexOf('iPad') > -1) {//iPad
    UA = "iPad";
  }
  else if (u.indexOf('Windows NT 5.1') > -1) {//xp
    UA = "Win Xp";
  }
  else if (u.indexOf('Windows NT 6.1') > -1) {//Win7
    UA = "Win7";
  }
  else if (u.indexOf('Windows NT 6.3') > -1) {//Win8
    UA = "Win8";
  }
  else if (u.indexOf('Windows NT 10.0') > -1) {//Win10
    UA = "Win10";
  }
  else if (u.indexOf('Mac OS') > -1) {//Win10
    UA = "Mac OS";
  }
  return UA;
}

//xhr表单上传文件
const fileUpload=function(formObj,postUrl,imgArray){
  Loading.open("图片上传中....");
  let xhrObj = new XMLHttpRequest();
  xhrObj.withCredentials = true;
  xhrObj.timeout=200000;
  let newForm = new FormData(document.getElementById(formObj));
  xhrObj.addEventListener('load', (res)=> {
    let data=JSON.parse(xhrObj.response);

    if(data.success){
      let resultUrl=data.result.picList;
      for(var a=0;a<resultUrl.length;a++){
        imgArray.push(resultUrl[a]);
      }
    }

    Toast({mes:data.msg,timeout:1000});
    Loading.close();
  });

  xhrObj.addEventListener('error',(res)=> {
    Toast({mes:'上传失败！',timeout:1000});
    Loading.close();
  });

  xhrObj.addEventListener('abort',(res)=>  {
    Toast({mes:'上传中断！',timeout:1000});
    Loading.close();
  });
  xhrObj.open('POST',postUrl);
  xhrObj.send(newForm);
};

export  {
  getCookie,
  getUA,
  fileUpload
}
